<?php
/* 菜单控制器, 处理后台管理端菜单相关的请求 */

namespace App\Http\Controllers;

use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class MenuController extends HomeController
{
    //菜单操作权限，1=新增 2=删除 3=修改 4=查看
    public $action = [1 => '新增', 2 => '删除', 3 => '修改', 4 => '查看'];
    public $status = [1 => '正常', -1 => '禁用'];

    public function __construct(Request $request, Menu $model)
    {
        parent::__construct();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取当前登录人员所拥有的菜单权限
     */
    public function menu(Request $request) {
        $list = array();
        $data = null;

        $user_id = $this->user_id;
        //根据用户id查询用户角色
        $role_id = DB::table('sys_user')->where('id', $user_id)->value('role_id');


        $cond = [];
        $cond['role_id'] = $role_id;
        //根据登录账号角色，列出有权限的菜单及动作
        $is_menus = DB::table("sys_role_menu")->where($cond)->get(["menu_id"]);
        if ($is_menus) {
            $menus_info = $is_menus->toArray();
            $menus_ids = array();
            foreach ($menus_info as $k =>$v){
                // 菜单id组合，用于展示菜单列表
                $menus_ids[]=$v->menu_id;
            }
        }
        //展示菜单
        $where['status'] = 1;
        $fields = ["id", "parent_id", "name", "path", "fontFamily", "icon"];
        //这里必须要先按parent_id排序，不然在处理树形菜单遍历数据时，会丢失二级菜单
        $data = Menu::where($where)->whereIn("id", $menus_ids)->orderBy('parent_id', 'asc')->orderBy('sort', 'asc')->get($fields)->toArray();

        $list['code'] = HTTP_OK;
        //把带单整理成树形菜单
        $tree = $this->getTree($data, 0);
        foreach ($tree as $k => $v) {
            if ($k == 0) {
                //第一个菜单默认展开和选中
                $tree[$k]['spread'] = true;
                $tree[$k]['isCheck'] = true;
            }
        }
        $list['data'] = $tree;
        $list['message'] = HTTP_OK_MESSAGE;
        return response()->json($list);
    }

    //获取系统所有的菜单列表及动作权限 用于添加和修改角色信息时，确定角色的菜单权限
    function sys_menu_list(Request $request){
        $list = array();
        //查询菜单
        $where['status'] = 1;
        $fields = ["id", "parent_id", "name", "path", "fontFamily", "icon", "action"];
        $data = Menu::where('status',1)->orderBy('parent_id', 'asc')->orderBy('sort', 'asc')->get($fields)->toArray();

        if (empty($data)) {
            $list['code'] = HTTP_ERROR;
            $list['message'] = HTTP_WRONG_MESSAGE;
            return response()->json($list);
        }

        //把带单整理成树形菜单
        $list['data'] = $this->getTree($data, 0);
        $list['code'] = HTTP_OK;
        $list['message'] = HTTP_OK_MESSAGE;
        return response()->json($list);
    }


    //获取当前登录用户对某个菜单的权限及菜单的基本信息
    public function action_by_path(Request $request) {
        $list = array();
        $path = $request->input('path');
        $user_id = $this->user_id;
        //根据用户id查询用户角色id
        $role_id = DB::table('sys_user')->where('id', $user_id)->value('role_id');
        //根据路径查询菜单id
        $menu_id = DB::table("sys_menu")->where('path', $path)->value('id');

        //查询该角色对该菜单的具体动作权限
        $action = DB::table("sys_role_menu")->where('role_id', $role_id)->where('menu_id', $menu_id)->value('action');

        $list['code'] = HTTP_OK;
        $list['data'] = $action;
        $list['message'] = HTTP_OK_MESSAGE;
        return response()->json($list);
    }



    //把菜单数组，转换成树状数组
    public function getTree($data, $pId)
    {
        $menu = array();
        //遍历数组
        foreach ($data as $k => $v) {
            //如果当前数据的parent_id等于传入的pId，则说明是找到了子级
            if ($v['parent_id'] == $pId) {
                $v['title'] = $v['name'];
                $v['href'] = $v['path'];
                $menu[$v['id']] = $v;
                $menu[$v['id']]['children'] = $this->getTree($data, $v['id']);
            }
        }

        return array_values($menu);

    }
   

}
